package top.sdaily.message.push;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jiguang.common.resp.DefaultResult;
import cn.jpush.api.JPushClient;
import cn.jpush.api.device.OnlineStatus;
import cn.jpush.api.device.TagAliasResult;

public class DeviceExample {
    protected static final Logger LOG = LoggerFactory.getLogger(DeviceExample.class);

    private static final String appKey = "dd1066407b044738b6479275";
    private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7";
    private static final String TAG1 = "tag1";
    private static final String ALIAS1 = "alias1";
    private static final String ALIAS2 = "alias2";
    private static final String REGISTRATION_ID1 = "0900e8d85ef";
    private static final String REGISTRATION_ID2 = "0a04ad7d8b4";

    private static JPushClient jpushClient = new JPushClient(masterSecret, appKey);

    public static void main(String[] args) {
        testGetDeviceTagAlias();
//		testGetUserOnlineStatus();
    }

    public static void testGetDeviceTagAlias() {
        try {
            TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1);

            LOG.info(result.alias);
            LOG.info(result.tags.toString());

        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);

        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
        }
    }

    public static void testGetUserOnlineStatus() {
        try {
            Map<String, OnlineStatus> result =  jpushClient.getUserOnlineStatus(REGISTRATION_ID1, REGISTRATION_ID2);

            LOG.info(result.get(REGISTRATION_ID1).toString());
            LOG.info(result.get(REGISTRATION_ID2).toString());
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
        }
    }

    public static void testBindMobile() {
        try {
            DefaultResult result =  jpushClient.bindMobile(REGISTRATION_ID1, "13000000000");
            LOG.info("Got result " + result);
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
        }
    }

}
